package com.example.controller;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Switch;

import com.example.listenanddriveproject.R;

public class MorphologieFragment extends Fragment {
	
	private Context context;
	private TextView textDilatation;
	private TextView textErosion;
	private Switch dilatationSwitch;
	private SeekBar seekBarDilatation;
	private Switch erosionSwitch;
	private SeekBar seekBarErosion;
	private Button button;
	private boolean boolDilatation;
	private int progressDilatation;
	private boolean boolErosion;
	private int progressErosion;
	
	public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		
		View rootView = inflater.inflate(R.layout.fragment_morphologie, container, false);
		
		context = this.getActivity();
		
		dilatationSwitch = (Switch) rootView.findViewById(R.id.switchDilatation);
		dilatationSwitch.setChecked(MainActivity.settings.getDilateSettings().isUsed());
		dilatationSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				boolDilatation=isChecked;
			}       

		});
		
		textDilatation = (TextView) rootView.findViewById(R.id.textViewDilatation);
		textDilatation.setText(String.valueOf(MainActivity.settings.getDilateSettings().getNbIteration()));
		
		seekBarDilatation=(SeekBar) rootView.findViewById(R.id.seekBarDilatation);
		seekBarDilatation.setProgress(MainActivity.settings.getDilateSettings().getNbIteration());
	    seekBarDilatation.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		    	textDilatation.setText(String.valueOf(progress));
		    	progressDilatation=progress;
		    }       
	    }); 
	    
	    erosionSwitch = (Switch) rootView.findViewById(R.id.switchErosion);
	    erosionSwitch.setChecked(MainActivity.settings.getErodeSettings().isUsed());
	    erosionSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				boolErosion=isChecked;
			}       

		});
	    
	    textErosion = (TextView) rootView.findViewById(R.id.textViewErosion);
		textErosion.setText(String.valueOf(MainActivity.settings.getErodeSettings().getNbIteration()));
	    
	    seekBarErosion=(SeekBar) rootView.findViewById(R.id.seekBarErosion);
	    seekBarErosion.setProgress(MainActivity.settings.getErodeSettings().getNbIteration());
	    seekBarErosion.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		    	textErosion.setText(String.valueOf(progress));
		    	progressErosion=progress;
		    }       
	    });
	    
	    button = (Button) rootView.findViewById(R.id.apply1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	MainActivity.settings.getDilateSettings().setUsed(boolDilatation);
            	MainActivity.settings.getDilateSettings().setNbIteration(progressDilatation);
            	
            	MainActivity.settings.getErodeSettings().setUsed(boolErosion);
            	MainActivity.settings.getErodeSettings().setNbIteration(progressErosion);
            	
            	MainActivity.settings.saveArray(context);
            }
        });

		return rootView;
	}
	
	/***
	 * refresh the UI when the reset is called
	 */
	@Override
	public void onResume(){
		super.onResume();
		
		if(MainActivity.settings.getDilateSettings().isRefresh()){
			dilatationSwitch.setChecked(MainActivity.settings.getDilateSettings().isUsed());
			textDilatation.setText(String.valueOf(MainActivity.settings.getDilateSettings().getNbIteration()));
			seekBarDilatation.setProgress(MainActivity.settings.getDilateSettings().getNbIteration());
			
			MainActivity.settings.getDilateSettings().setRefresh(false);
		}
		if(MainActivity.settings.getErodeSettings().isRefresh()){
			erosionSwitch.setChecked(MainActivity.settings.getErodeSettings().isUsed());
			textErosion.setText(String.valueOf(MainActivity.settings.getErodeSettings().getNbIteration()));
			seekBarErosion.setProgress(MainActivity.settings.getErodeSettings().getNbIteration());
			
			MainActivity.settings.getErodeSettings().setRefresh(false);
		}
	}

}
